<?php

/**
 * alltosun.com 终端设备控制器 index.php
 * ============================================================================
 * 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 刘德洪 (Liudh@alltosun.com) $
 * $Date: 20151124 $
 * $Id$
 */
class Action
{
    private $per_page = 100;

    /**
     * 显示列表
     */
    public function index() {

        $page = Request::Get('page_no', 1);
        $status = Request::getParam('status', -1);
        $search_filter = Request::getParam('search_filter', array());

        // 拼装搜索条件
        $filter = array();

        //姓名
        if (isset($search_filter['dealer_name']) && $search_filter['dealer_name']) {
            $userinfo = _model('user')->getList(array('realname' => $search_filter['dealer_name']));
            if (empty($userinfo)) {
                return '未查到用户ID';
            }
            $userinfo = $userinfo[0];
            $filter['parent_id'] = $userinfo['id'];
        }

        //店铺名称
        $list = array();
        $storefilter = array();
        if (isset($search_filter['title']) && $search_filter['title']) {
            $storefilter['title'] = $search_filter['title'];
        }

        //地域筛选
        if (isset($search_filter['district_id']) && $search_filter['district_id']) {

            $storefilter['district_id'] = $search_filter['district_id'];
            $area = _widget('area')->get_area_info_by_area_code($search_filter['district_id']);
            Response::assign('area', $area);
        } elseif (isset($search_filter['city_id']) && $search_filter['city_id']) {

            $storefilter['city_id'] = $search_filter['city_id'];
            $area = _widget('area')->get_area_info_by_area_code($search_filter['city_id']);
            Response::assign('area', $area);
        } elseif (isset($search_filter['province_id']) && $search_filter['province_id']) {

            $storefilter['province_id'] = $search_filter['province_id'];
            $area = _widget('area')->get_area_info_by_area_code($search_filter['province_id']);
            Response::assign('area', $area);
        } else {

        }


        if ($storefilter) {
            $storeinfo = _model('store')->getList($storefilter);

            if (!empty($storeinfo)) {

                foreach ($storeinfo as $k => $v) {
                    array_push($list, $v['id']);
                }
                $filter['`store_id`'] = $list;
            } else {
                $filter['`store_id`'] = '-1';//所输条件没有查到店铺
            }
        }

        if (-1 != $status) {
            $filter['status'] = $status;
        } else {
            $filter['status >='] = '0';
        }

        $photo_list = get_data_list('topic_olympic', $filter, 'ORDER BY upload_time DESC', $page, $this->per_page);

        if ($photo_list) {
            foreach ($photo_list as $k => $v) {
                //                $device_list[$k]['device_no'] = $v[$res_name . '_no'];
                //                if ($v['status'] == 2) {
                //                    $relation = array(
                //                        'res_id' => $v['id'],
                //                        'res_name' => $res_name,
                //                    );
                //                    $relation_info = _uri('store_device_relation', $relation);
                $store_info = store_helper::get_store($v['store_id']);
                if ($store_info) {
                    $province_info = area_helper::get_area_info(@$store_info['province_id']);
                    $city_info = area_helper::get_area_info(@$store_info['city_id']);
                    $county_info = area_helper::get_area_info(@$store_info['district_id']);
                    $store_info['province'] = @$province_info['area'];
                    $store_info['city'] = @$city_info['area'];
                    $store_info['county'] = @$county_info['area'];
                } else {
                    $store_info['province'] = '';
                    $store_info['city'] = '';
                    $store_info['county'] = '';
                }
                $photo_list[$k]['store_info'] = $store_info;

                $dealer_info = user_helper::get_user_info($v['parent_id']);
                if ($dealer_info) {
                    $photo_list[$k]['dealer_info'] = $dealer_info;
                }
            }
        }

        Response::assign('status', $status);
        Response::assign('photo_list', $photo_list);
        Response::assign('search_filter', $search_filter);
        Response::display('admin/list.html');
    }

    /**
     * 修改状态
     */
    public function change_status() {
        $id = Request::getParam('id', 0);
        $status = Request::getParam('status', 0);

        $page = Request::Get('page_no', 1);
        $search_filter = Request::getParam('search_filter', array());
        if (!$id) {
            return '请选择要更改的内容';
        }

        //        $info = _uri('topic_olympic', $id);
        //        if ($status == $info['status']) {
        //            return '已经执行过此操作，不用重复提交';
        //        }

        _model('topic_olympic')->update($id, array('status' => $status));


        return 'ok';
    }


    public function export_csv($filename, $data) {
        $file_hwnd = fopen('' . $filename, "w");
        header("Content-type:text/csv");
        header("Content-Disposition:attachment;filename=" . $filename);
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
        header('Expires:0');
        header('Pragma:public');
        fwrite($file_hwnd, $data);
        fclose($file_hwnd);
    }

    public function export() {

        $export_date = Request::getParam('export_date', 0);

        $filter = array(
            '1' => 1,
        );

        $photo_list = _model('topic_olympic')->getList($filter, 'ORDER BY upload_time DESC');
        if (count($photo_list) > 20000) {
            $this->error('导出数据过多会导致内存溢出！请分别查询导出！');
        }
        //$head[] = iconv('utf-8','gb2312',",序号,上传时间,合伙人,商铺ID,商铺名称,状态\r\n");

        echo iconv('utf-8', 'gb2312//IGNORE', "序号,合伙人,商铺ID,商铺名称,上传时间,状态\n");
        $row = array();
        if ($photo_list) {
            foreach ($photo_list as $k => $v) {
                $store_info = store_helper::get_store($v['store_id']);

                $photo_list[$k]['store_info'] = $store_info['title'] . "-" . $store_info['title_branch'];

                $dealer_info = user_helper::get_user_info($v['parent_id']);
                if (empty($dealer_info)) {
                    $dealer_info['realname'] = "无名氏";
                }

                //  $content[] = iconv('utf-8','gb2312',$v['id']).",".
                //               iconv('utf-8','gb2312',$v['upload_time']).",".
                //               iconv('utf-8','gb2312',$dealer_info['realname']).",".
                //               iconv('utf-8','gb2312',$v['store_id']).",".
                //               iconv('utf-8','gb2312',$photo_list[$k]['store_info']).",".
                //               iconv('utf-8','gb2312',$v['status']).","."\r\n";

                $row[] = join(',', array(
                    $v['id'],
                    $dealer_info['realname'],
                    $v['store_id'],
                    str_replace(array("\n", "\t", "\r"), '', $photo_list[$k]['store_info']),
                    $v['upload_time'],
                    $v['status'],
                ));
            }
        }
        echo iconv('UTF-8', 'GBK//IGNORE', join("\n", $row));
        //$str = implode(',',array_merge($head, $content));
        $this->export_csv(time() . '.csv', 0);
        //$this->success('导出完成',U('Beacon/download', array('backup'=>'runtime', 'file'=>"$csvname")));
    }

    public function download() {
        $topic_name = Request::Get('topic_name', '');
        if (empty($topic_name) || !in_array($topic_name, topic_config::$TOPIC)) {
            return false;
        }
        return _widget('topic.data')->download($topic_name);
    }
}